package com.xyh.transaction.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.data.WriteCellData;

public class GenderConverter implements Converter<Integer> {

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        Integer value = context.getValue();

        if (value == null) {
            return new WriteCellData<>("");
        }

        if (value.equals(0)) {
            return new WriteCellData<>("女");
        }

        return new WriteCellData<>("男");
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        String value = context.getReadCellData().getStringValue();
        if ("".equals(value)) {
            return null;
        }
        if ("男".equals(value)) {
            return 1;
        }
        return 0;
    }
}
